package com.agriculture.data_demo.service;

import com.agriculture.data_demo.dto.PageResult;
import com.agriculture.data_demo.entity.sys.HogBatchEntity;
import com.agriculture.data_demo.entity.sys.HogDailyEntity;
import com.agriculture.data_demo.entity.sys.PigEnvEntity;
import com.agriculture.data_demo.repository.HogBatchRepository;
import com.agriculture.data_demo.repository.HogDailyRepository;
import com.agriculture.data_demo.repository.PigEnvRepository;
import com.agriculture.data_demo.vo.sys.HogBatchVo;
import com.agriculture.data_demo.vo.sys.HogDailyVo;
import com.agriculture.data_demo.vo.sys.PigEnvVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author morningbear
 * @date 20.10.7
 */
@Service
@Slf4j
public class HogService {

    @Resource
    private HogBatchRepository hogBatchRepository;
    @Resource
    private HogDailyRepository hogDailyRepository;
    @Resource
    private PigEnvRepository pigEnvRepository;

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 所有猪场环境数据
     */
    public PageResult<PigEnvVo> getAllPigEnv(Integer pageNumber, Integer pageSize){
        Page<PigEnvEntity> page = pigEnvRepository.findAll(PageRequest.of(pageNumber, pageSize));

        List<PigEnvVo> pigEnvVoList = new ArrayList<>();
        for (PigEnvEntity pigEnvEntity : page.getContent()){
            PigEnvVo pigEnvVo = new PigEnvVo();
            BeanUtils.copyProperties(pigEnvEntity, pigEnvVo);
            pigEnvVoList.add(pigEnvVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), pigEnvVoList);
    }

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 所有肉猪批次数据
     */
    public PageResult<HogBatchVo> getAllHogBatch(Integer pageNumber, Integer pageSize){
        Page<HogBatchEntity> page = hogBatchRepository.findAll(PageRequest.of(pageNumber, pageSize));

        List<HogBatchVo> hogBatchVoList = new ArrayList<>();
        for (HogBatchEntity hogBatchEntity : page.getContent()){
            HogBatchVo hogBatchVo = new HogBatchVo();
            BeanUtils.copyProperties(hogBatchEntity, hogBatchVo);
            hogBatchVoList.add(hogBatchVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), hogBatchVoList);
    }

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 所有肉猪生产周期数据
     */
    public PageResult<HogDailyVo> getAllHogDaily(Integer pageNumber, Integer pageSize){
        Page<HogDailyEntity> page = hogDailyRepository.findAll(PageRequest.of(pageNumber, pageSize));

        List<HogDailyVo> hogDailyVoList = new ArrayList<>();
        for (HogDailyEntity hogDailyEntity : page.getContent()){
            HogDailyVo hogDailyVo = new HogDailyVo();
            BeanUtils.copyProperties(hogDailyEntity, hogDailyVo);
            hogDailyVoList.add(hogDailyVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), hogDailyVoList);
    }
}
